package com.cos.controller.app.appuser;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cos.controller.base.BaseController;
import com.cos.service.interfaces.dynamicstatus.DynamicStatusService;
import com.cos.util.AppUtil;
import com.cos.util.PageData;
import com.cos.util.ResponseUtil;


/**
 * 
 * 
 * 赞 踩接口
 * <p/>
 * 
 * @author <a href="1048688506@qq.com">Yang</a>
 * @version  Date: 2016年3月31日 下午9:02:50
 * @serial 1.0
 * @since 2016年3月31日 下午9:02:50
 */
@Controller
@RequestMapping(value="/appuser")
public class IntDynamicStatusController extends BaseController{
	
	@Resource
	private DynamicStatusService dynamicStatusService ;
	/**
	 * 记录用户对动态的赞记录接口
	 * result = "01" 用户赞失败 ，参数不合法
	 * result = "02" 用户赞成功
	 * result = "03" 用户赞失败 服务器异常响应
	 * result = "04" 用户已对此条动态赞过
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="saveSupport")
	@ResponseBody
	public void saveSupport(HttpServletResponse response) throws Exception{
		logBefore(logger, "保存用户赞状态");
		PageData map = new PageData() ;
		PageData pd = new PageData() ;
		JSONObject SUPPORT = new JSONObject() ;
		pd = this.getPageData() ;
		int  SUPPORT_NUM =Integer.parseInt(pd.getString("SUPPORT_NUM")) ;
		map.put("USER_ID", pd.getString("USER_ID"))  ;
		map.put("DT_ID", pd.getString("DT_ID"))  ;
		map.put("DT_TYPE", pd.getString("DT_TYPE"))  ;
		map.put("SUPPORT_NUM",++SUPPORT_NUM)  ;
		String result = "00"  ;
		try{
			if(AppUtil.checkParam("saveSupport", pd)){
				if(dynamicStatusService.getDynamicStatusById(map)!=null){
					if(Integer.parseInt(String.valueOf(dynamicStatusService.getDynamicStatusById(map).get("SUPPORT")))==1){
						result = "04" ; //已经点击过赞
					}
				}else{
					pd.put("ID", get32UUID()) ;
					dynamicStatusService.saveSupport(pd);
					dynamicStatusService.updateDynamic(map);
					result = "02"  ;//用户点赞成功
				}
			}else{
					result = "01" ; //用户点赞失败 非法的参数录入
			}
		}catch (Exception e){
			e.printStackTrace(); 
			logger.error(e.toString());
			result = "03" ; //服务器异常
		
		}finally{
			
			SUPPORT.put("result", result) ;
		}
		ResponseUtil.write(response, SUPPORT);
	}
	/**
	 * 记录用户对动态的踩记录接口
	 * result = "01" 用户踩失败 ，参数不合法
	 * result = "02" 用户踩成功
	 * result = "03" 用户踩失败 服务器异常响应
	 * result = "04" 用户已对此条动态踩过
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="saveTrample")
	@ResponseBody
	public void saveTrample(HttpServletResponse response ) throws Exception{
		logBefore(logger, "保存用户踩状态");
		PageData map = new PageData() ;
		PageData pd = new PageData() ;
		JSONObject TRAMPLE = new JSONObject() ;
		pd = this.getPageData() ;
		int  TRAMPLE_NUM =Integer.parseInt(pd.getString("TRAMPLE_NUM")) ;
		map.put("USER_ID", pd.getString("USER_ID"))  ;
		map.put("DT_ID", pd.getString("DT_ID"))  ;
		map.put("DT_TYPE", pd.getString("DT_TYPE"))  ;
		map.put("TRAMPLE_NUM", ++TRAMPLE_NUM)  ;
		String result = "00"  ;
		try{
			if(AppUtil.checkParam("saveTrample", pd)){
				if(dynamicStatusService.getDynamicStatusById(map)!=null){
					if(Integer.parseInt(String.valueOf(dynamicStatusService.getDynamicStatusById(map).get("TRAMPLE")))==1){
						result = "04" ; //已经点击过踩
						}
					}else{
					pd.put("ID", get32UUID()) ;
					dynamicStatusService.saveTrample(pd);
					dynamicStatusService.updateDynamic(map);
					result = "02"  ;//用户点踩成功
				}
			}else{
					result = "01" ; //用户点踩失败 非法的参数录入
			}
		}catch (Exception e){
			e.printStackTrace(); 
			logger.error(e.toString());
			result = "03" ; //服务器异常
		
		}finally{
			
			TRAMPLE.put("result", result) ;
		}
		ResponseUtil.write(response, TRAMPLE);
	}
	
	
	
}
